import Vue from 'vue'
import Vuex from 'vuex'
import axios from 'axios'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    city_list:[],
    ticket_list:[],
    search_ticket_list:[], //搜索结果列表
  },
  mutations: {
    SET_CITY_LIST(state,data){
      state.city_list=data;
    },

    SET_TICKET_LIST(state,data){
      state.ticket_list=data;
    },
    // 搜索票
    SEARCH_TICKET(state,data){
       if(data.from && data.to){
        state.search_ticket_list=state.ticket_list.filter((item)=>{
          return item.to===data.to && item.from ===data.from;
        })
       }else if(data.from){
         state.search_ticket_list=state.ticket_list.filter((item)=>{
           return item.from=data.from;
         })
       }else if(data.to){
        state.search_ticket_list=state.ticket_list.filter((item)=>{
          return item.to=data.to;
        })
      }else{
        state.search_ticket_list=state.ticket_list;
      }
    }
  },
  actions: {
    getCityList(context){
      axios.get('/api/city').then((res)=>{
        console.log('city res',res);
           context.commit("SET_CITY_LIST",res.data.data.list);
      })
    },

    getTicketList(context){
      axios.get('/api/ticket').then((res)=>{
        console.log('ticket res',res);
        context.commit("SET_TICKET_LIST",res.data.data.list);
      })
    }
  },
  modules: {
  }
})
